import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync('token') || "",
		userInfo: uni.getStorageSync('userInfo') || {},
		storeaddcs: JSON.parse(uni.getStorageSync('storeaddcs') || '{}'),
		searchHistory: JSON.parse(uni.getStorageSync('searchHistory') || '[]'),
		cart: JSON.parse(uni.getStorageSync('cart') || '[]'),
		sumNumber: JSON.parse(uni.getStorageSync('sumNumber') || 0),
		sumPrice: JSON.parse(uni.getStorageSync('sumPrice') || 0)
	},
	mutations: {
		login(state, provider) {
			state.userInfo = provider;
			uni.setStorage({//缓存用户登陆状态
			    key: 'userInfo',  
			    data: provider  
			}) 
		},
		saveToken (state,token) {
			state.token = token;
			uni.setStorage({//缓存用户登陆状态
			    key: 'token',  
			    data: token  
			}) 
		},
		ChangeAddcs (state,item) {
			state.storeaddcs = item
			this.commit('LoseorageAddcs')
		},
		LoseorageAddcs (state) {
			 uni.setStorageSync('storeaddcs',JSON.stringify(state.storeaddcs))
		},
		loginOut (state) {
			this.state.userInfo = {}
			this.state.token = ''
			this.commit('login',{})
			this.commit('saveToken','')
		},
		/*
					改变商品数量
				*/
		changeShopNumebr (state,item) {
				  state.cart.forEach((item2) => {
					  if (item2.id === item.id) {
						  item2.number = item.number
					  }
				  })
				  this.commit('losorageCart')
				  this.commit('computedShopPrice')
				  this.commit('computedShopNumber')
		},
				/*
					删除购物车单个商品
				*/
		delOneShop (state,item) {
				 const findResult = state.cart.findIndex((item2) => {return item2.id === item.id})
				 if (findResult === -1) return
					state.cart.splice(findResult,1)
					this.commit('losorageCart')
					this.commit('computedShopPrice')
					this.commit('computedShopNumber')
		},
				/*
					选择单个商品
				*/
		changeOneShop (state,item) {
				 state.cart.forEach((item2) => {
					 if (item2.id === item.id) {
						 item2.is_sel = !item2.is_sel
					 }
				 })
				   let flagCart = state.cart
				   state.cart = []
				   state.cart = flagCart
				 this.commit('losorageCart')
				 this.commit('computedShopPrice')
				 this.commit('computedShopNumber')
		},
				/*
					清空购物车
				*/
		closeAllCart (state) {
					let list = []
					state.cart.forEach((item) => {
						if (!item.is_sel && item.is_del === '0') list.push(item)
					})
					state.cart = list
				   this.commit('losorageCart')
				   this.commit('computedShopPrice')
				   this.commit('computedShopNumber')
		},
				/*
					用户全选
				*/
		userAllCartOk (state) {
				   state.cart.forEach((item) => {
				   	 item.is_sel = true
				    })
					let flagCart = state.cart
					state.cart = []
					state.cart = flagCart
				   this.commit('losorageCart')
				   this.commit('computedShopPrice')
				   this.commit('computedShopNumber')
		},
				/*
					用户全部不选
				*/
		userAllCartOver (state) {
				 state.cart.forEach((item) => {
					 item.is_sel = false
				 })
				 let flagCart = state.cart
				 state.cart = []
				 state.cart = flagCart
				this.commit('losorageCart')
				this.commit('computedShopPrice')
				this.commit('computedShopNumber')
		},
				/*
					计算商品价格
				*/
		computedShopPrice (state) {
					let sum = 0
				 state.cart.forEach((item) => {
					 sum += (item.number * item.price)
				 })
				   state.sumPrice = sum
				   this.commit('losorageSumPrice')
		},
		/*
					计算商品数量
		*/
				computedShopNumber (state) {
					let sum = 0
					state.cart.forEach((item) => {
								 sum += item.number
					})
					  state.sumNumber = sum
					  this.commit('losorageSumNumber')
				},
				/*
					存储商品的总价格
				*/
		losorageSumPrice (state) {
				   	uni.setStorageSync('sumPrice',JSON.stringify(state.sumPrice))
		},
				/*
					存储商品的数量
				*/
		losorageSumNumber (state) {
				   uni.setStorageSync('sumNumber',JSON.stringify(state.sumNumber))
		},
				/*
					加入购物车
				*/
				addCart (state,item) {
					const findResult = state.cart.findIndex((item2) => {return item2.id === item.id})
					if (findResult === -1) {
						item.number = 1
						item.is_sel = true
						state.cart.push(item)
						this.commit('losorageCart')
						this.commit('computedShopPrice')
						this.commit('computedShopNumber')
					} else {
						console.log(state.cart[findResult])
						state.cart[findResult] = state.cart[findResult]
						state.cart[findResult].number++
						console.log(state.cart[findResult])
						this.commit('losorageCart')
						this.commit('computedShopPrice')
						this.commit('computedShopNumber')
					}
				},
				/*
					加入购物车存储
				*/
		losorageCart (state) {
				   uni.setStorageSync('cart',JSON.stringify(state.cart))
		},
				closeOneSearchHistory(state,index) {
					state.searchHistory.splice(index,1)
					this.commit('loseorageSearchHistory') 
				},
				closeAllSearchHistory (state) {
					state.searchHistory = []
					this.commit('loseorageSearchHistory') 
				},
				changeSearchHistory (state,item) {
					let findResult = state.searchHistory.findIndex((item2) => {return item2 === item})
					if (findResult === -1) {
						state.searchHistory.length === 0 ? state.searchHistory.unshift(item) : state.searchHistory.unshift(item)
						this.commit('loseorageSearchHistory')
						
					} else {
						state.searchHistory.splice(findResult,1)
						state.searchHistory.length === 0 ? state.searchHistory.unshift(item) : state.searchHistory.unshift(item)
						this.commit('loseorageSearchHistory')
					}
				},
				loseorageSearchHistory (state) {
					uni.setStorageSync('searchHistory',JSON.stringify(state.searchHistory))
				},
				ChangeAddcs (state,item) {
					state.storeaddcs = item
					this.commit('LoseorageAddcs')
				},
				LoseorageAddcs (state) {
					 uni.setStorageSync('storeaddcs',JSON.stringify(state.storeaddcs))
				}
	},
	actions: {
	
	},
	getters: {
		sumNumber(state){
			let sum = 0
			state.cart.forEach((item) => {
				if (!item.is_sel) return
				else sum += item.number
			})
			state.sumNumber = sum
			return state.sumNumber
		},
		sumPrice(state){
			let sumShopPrice = 0
			state.cart.forEach((item) => {
				if (!item.is_sel) return
				else sumShopPrice += (item.number * item.price)
			})
			state.sumPrice = sumShopPrice
			return state.sumPrice
		}
	}
})

export default store
